package nl.nhl.idp.Views.StatusView;

import java.util.ArrayList;
import nl.nhl.idp.R;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

/**
 * Adapter to convert ListViewItems for display in a StatusListView
 * @author Alex Wijnbeek
 */
public class ListItemAdapter extends ArrayAdapter<ListViewItem>
{
	private int textSize = 10;
	private Context context;
	private int layoutResourceId;
	private ArrayList<ListViewItem> data = null;

	/**
	 * Sets the font size to use in the statusview
	 * @param size
	 */
	public void SetTextSize(int size)
	{	
		textSize = size;
	}
	
	/**
	 * Creates a new ListItemAdapter
	 * @param context
	 * @param layoutResourceId
	 * @param data
	 */
	public ListItemAdapter(Context context, int layoutResourceId,
			ArrayList<ListViewItem> data)
	{
		super(context, layoutResourceId, data);
		this.layoutResourceId = layoutResourceId;
		this.context = context;
		this.data = data;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{
		View row = convertView;
		TextView textName = null;
		TextView textValue = null;
		
		//get layout resource
		if (row == null)
		{
			LayoutInflater inflater = ((Activity) context).getLayoutInflater();
			row = inflater.inflate(layoutResourceId, parent, false);
		}
		
		//initialize textviews based on layout
		textName = (TextView)row.findViewById(R.id.listViewItem_Name);
		textValue = (TextView)row.findViewById(R.id.listViewItem_Value);

		//set text size
		textName.setTextSize(TypedValue.TYPE_STRING, textSize);
		textValue.setTextSize(TypedValue.TYPE_STRING, textSize);
		
		//if the current ListViewItem is the topmost one, treat it like a header!
		if(position == 0)
		{
			textName.setBackgroundColor(Color.parseColor("#2F1E0C"));
			textName.setTextColor(Color.parseColor("#E4CDB5"));
			textValue.setBackgroundColor(Color.parseColor("#2F1E0C"));
			textValue.setTextColor(Color.parseColor("#E4CDB5"));
		}
		else //it's not a header (duh~)
		{
			textName.setTextColor(Color.parseColor("#E4CDB5"));
			textValue.setTextColor(Color.parseColor("#E4CDB5"));
		}
		
		//get ListViewItem to display
		ListViewItem item = data.get(position);
		
		//set the values
		textName.setText(item.GetName());
		textValue.setText(item.GetValue()+" "+item.GetUnit());
		
		//return the created ListView row;
		return row;
	}
}